import { ref, computed } from 'vue'
import { defineStore } from 'pinia'
import type { User, UserAttr } from '@/modle/User'
export const useCounterStore = defineStore('counter', () => {
  
  const count = ref(0)
  const user = ref<UserAttr>(null)

  const doubleCount = computed(() => count.value * 2)

  function increment() {
    count.value++
  }

  const setUser = (use:UserAttr) => {
    user.value = use;
  }
  const getUser = ():UserAttr => {
    return user.value;
  }
  const logout = () => {
    user.value = null;
  }
  const isLogin = ():boolean => {
    return user.value !== null
  }
  const  changeH = (h:string) => {
    user.value!.headPortrait = h;
  }
  const changeName = (name:string) => {
    user.value!.userName = name
  }
  const changeAddr = (addr:string) => {
    user.value!.shippingAddress = addr
  }
  const changeSex = (sex:any) => {
    sex == 1? user.value!.sex = '男': user.value!.sex='女'
  }
  const changePhoneNumber = (number:string) => {
    user.value!.phoneNumber = number
  }
  const changeRealName = (realName:string) => {
    user.value!.realName = realName
  }
  return { count, doubleCount, increment ,
    setUser,
    getUser,
    logout,
    isLogin,
    changeH,
    changeAddr,
    changeName,
    changeSex,
    changePhoneNumber,
    changeRealName}
})
